/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package matrix;

import Jama.*;

/**
 *
 * @author nikeadavis
 */
public class SchurFactorization {
    private Matrix A;
    private Matrix r1;
    private Matrix u1;
    private Matrix u2;
    private Matrix B;
    private Matrix U;
    private Matrix T;
    private double eigValue;

    
    public SchurFactorization(Matrix A, double eigValue){
        this.A=A;
        this.eigValue=eigValue;
        init();
    }

    private void init(){
        //Calculate B
        	Matrix eigI=Matrix.identity(A.getRowDimension(), A.getColumnDimension()).times(eigValue);
		B=A.minus(eigI);

        //r1
         r1=B.getMatrix(0, 0,0,1);

         //U or orthogonal matrix
         Matrix r1Perp=getPerpendicularVector(r1);
		u1= r1Perp.times(1/(r1.norm2()));
		 u2 = getPerpendicularVector(u1);
		
		U= new Matrix(2,2);
		U.set(0, 0, u1.get(0, 0));
		U.set(1, 0, u1.get(0, 1));
		U.set(0, 1, u2.get(0, 0));
		U.set(1, 1, u2.get(0, 1));

        //T
        Matrix qt=U.transpose();
		Matrix aqt=qt.times(A);
         T= aqt.times(U);
    }

 public Matrix getB(){
		return B;
	}

	public Matrix getT(){
		return T;
	}

        public Matrix getU(){
		return U;
	}

        private Matrix getPerpendicularVector(Matrix r){

		double p=r.get(0, 0);
		double q=-1*(r.get(0, 1));

		Matrix n=new Matrix(1,2);
		n.set(0, 0, q);
		n.set(0,1,p);

		return n;
	}
}
